<?php
// +----------------------------------------------------------------------
// | HkCms 自定义列表
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\template\controller;

use common\controller\AdminBase;
use libs\util\Form;
use app\template\validate\Customlist as CustomlistValidate;

class Customlist extends AdminBase {

    private $db = NULL;

    //初始
    public function _initialize() {
        parent::_initialize();
        $this->db = Db('Customlist');
    }

    /**
     * 列表首页
     */
    public function index() {
        return $this->basePage($this->db, '', array("id" => "desc"));
    }

    /**
     * 添加列表
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $rule = (new CustomlistValidate)->sceneBase();
            $result = $this->validate($_POST, $rule[0], $rule[1]);
            if(true !== $result){
                $this->error($result);
            }
            $data = $_POST;
            unset($data['isurltype']);
            $data['createtime'] = time();
            $id = Db('customlist')->insertGetId($data);
            if ($id) {
                //更新访问地址
                $urlArray = HkCMS()->Url->createListUrl((int) $id);
                if ($urlArray !== false) {
                    Db('customlist')->where(array('id' => $id))->update(array('url' => $urlArray['url']));
                }
                $this->success('添加成功！', url('index'));
            } else {
                $this->error('自定义列表添加失败！');
            }
        } else {
            $this->templateAndRule();
            return $this->fetch();
        }
    }

    //编辑
    public function edit() {
        if ($this->request->isPost()) {
            $rule = (new CustomlistValidate)->sceneBase();
            $result = $this->validate($_POST, $rule[0], $rule[1]);
            if(true !== $result){
                $this->error($result);
            }
            $data = $_POST;
            unset($data['isurltype']);
            $id = Db('customlist')->where(['id'=>$data['id']])->update($data);
            if ($id) {
                $this->success('修改成功！', url('index'));
            } else {
                $this->error('自定义列表修改失败！');
            }
        } else {
            $id = input('id', 0, 'intval');
            $info = $this->db->where(array('id' => $id))->find();
            if (empty($info)) {
                $this->error('该自定义列表不存在！');
            }

            $this->templateAndRule($info);
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

    //删除
    public function delete() {
        $id = input('get.id', 0, 'intval');
        if ($this->db->deleteCustomlist($id)) {
            $this->success('删除成功！');
        } else {
            $error = $this->db->getError();
            $this->error($error ? $error : '删除失败！');
        }
    }

    //生成列表
    public function generate() {
        if ($this->request->isPost()) {
            $ids = input('post.ids');
            if (empty($ids)) {
                $this->error('请指定需要生成的自定义列表！');
            }
            foreach ($ids as $id) {
                if ($this->Html->createListHtml((int) $id) == false) {
                    $this->error('生成失败！');
                }
            }
            $this->success('生成成功！');
        } else {
            $id = input('id', 0, 'intval');
            if (empty($id)) {
                $this->error('请指定需要生成的自定义列表！');
            }
            if ($this->Html->createListHtml((int) $id)) {
                $this->success('生成成功！');
            } else {
                $this->error('生成失败！');
            }
        }
    }
    /**
     * 初始模板和URL规则信息
     * @param array $info
     */
    private function templateAndRule($info = array('urlruleid' => '')) {
        $filepath = TEMPLATE_PATH . (empty(self::$Cache["Config"]['theme']) ? "default" : self::$Cache["Config"]['theme'])  . "/Content/";
        $tp_list = str_replace("{$filepath}List/", '', glob($filepath . 'List/list*'));
        $this->assign('list_html_ruleid', Form::urlrule('content', 'category', 1, $info['urlruleid'], 'name="urlruleid"'));
        $this->assign('tp_list', $tp_list);
    }

}
